package com.dh.leetcode.one;

import com.dh.leetcode.common.TreeNode;
import com.dh.leetcode.common.TreeNodeIterator;
import org.junit.Assert;
import org.junit.Test;

/**
 * @ClassName: _100_same_tree
 * @Description: 100. 相同的树
 * 给定两个二叉树，编写一个函数来检验它们是否相同。
 * <p>
 * 如果两个树在结构上相同，并且节点具有相同的值，则认为它们是相同的。
 * <p>
 * 示例 1:
 * <p>
 * 输入:       1         1
 * / \       / \
 * 2   3     2   3
 * <p>
 * [1,2,3],   [1,2,3]
 * <p>
 * 输出: true
 * 示例 2:
 * <p>
 * 输入:      1          1
 * /           \
 * 2             2
 * <p>
 * [1,2],     [1,null,2]
 * <p>
 * 输出: false
 * 示例 3:
 * <p>
 * 输入:       1         1
 * / \       / \
 * 2   1     1   2
 * <p>
 * [1,2,1],   [1,1,2]
 * <p>
 * 输出: false
 * <p>
 * https://leetcode-cn.com/problems/same-tree/
 * @Author: shouzimu
 * @Date: 2021/1/14 17:47
 */
public class _100_same_tree {

    /*
    错误代码示例
    判断前序和后续是否相同，如果存在值一样的话就不行了
    public boolean isSameTree(TreeNode p, TreeNode q) {
        List<Integer> pList = new ArrayList<>();
        leftOrder(p, pList);
        List<Integer> qList = new ArrayList<>();
        leftOrder(q, qList);
        int pl = pList.size();
        int ql = qList.size();
        if (pl != ql) {
            return false;
        }
        for (int i = 0; i < pl; i++) {
            if (!Objects.equals(pList.get(i), qList.get(i))) {
                return false;
            }
        }
        pList.clear();
        qList.clear();

        rightOrder(p, pList);
        rightOrder(q, qList);
        pl = pList.size();
        ql = qList.size();
        if (pl != ql) {
            return false;
        }
        for (int i = 0; i < pl; i++) {
            if (!Objects.equals(pList.get(i), qList.get(i))) {
                return false;
            }
        }

        return true;
    }

    public void leftOrder(TreeNode node, List<Integer> list) {
        if (node == null) {
            return;
        }
        if (node.left == null && node.right != null) {
            list.add(null);
        }
        leftOrder(node.left, list);
        list.add(node.val);
        leftOrder(node.right, list);
    }

    public void rightOrder(TreeNode node, List<Integer> list) {
        if (node == null) {
            return;
        }
        if (node.right == null && node.left != null) {
            list.add(null);
        }
        rightOrder(node.right, list);
        list.add(node.val);
        rightOrder(node.left, list);
    }
    */


    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (null == p && null != q) {
            return false;
        }
        if (null != p && null == q) {
            return false;
        }
        if (null == p && null == q) {
            return true;
        }
        if (p.val != q.val) {
            return false;
        }
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }

    @Test
    public void isSameTreeTest() {
        TreeNode p = TreeNodeIterator.initTestTree(new Integer[]{1, 2});
        TreeNode q = TreeNodeIterator.initTestTree(new Integer[]{1, null, 2});
        Assert.assertEquals(false, isSameTree(p, q));

        p = TreeNodeIterator.initTestTree(new Integer[]{1, null, 1});
        q = TreeNodeIterator.initTestTree(new Integer[]{1, null, 1});
        Assert.assertEquals(true, isSameTree(p, q));

        p = TreeNodeIterator.initTestTree(new Integer[]{1, 2, 3});
        q = TreeNodeIterator.initTestTree(new Integer[]{1, 2, 3});
        Assert.assertEquals(true, isSameTree(p, q));
    }
}
